package cn.yunhe.biz.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.yunhe.biz.IUserBiz;
import cn.yunhe.dao.UserMapper;
import cn.yunhe.entity.User;
import cn.yunhe.entity.UserExample;

@Service("userBiz")
public class UserBizImpl implements IUserBiz {
	
	@Autowired
	private UserMapper umapper;
	
	public void setUmapper(UserMapper umapper) {
		this.umapper = umapper;
	}

	@Override
	public User UserLogin(User user) {
		// TODO Auto-generated method stub
		List<User> list = null;
		try {
			UserExample example = new UserExample();
			UserExample.Criteria crita = example.createCriteria();
			crita.andULoginnameEqualTo(user.getuLoginname());
			crita.andUPasswordEqualTo(user.getuPassword());
			list = umapper.selectByExample(example);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list.get(0);
	}

	@Override
	public Integer UpdateUser(User user, Integer id) {
		// TODO Auto-generated method stub
		Integer flag = 0;
		try {
			UserExample example = new UserExample();
			UserExample.Criteria crita = example.createCriteria();
			crita.andRIdEqualTo(id);
			flag = umapper.updateByExample(user, example);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public List<User> FindAllUser(String uLoginname) {
		// TODO Auto-generated method stub
		List<User> list = null;
		try {
			UserExample example = new UserExample();
			UserExample.Criteria crita = example.createCriteria();
			if(StringUtils.isNotBlank(uLoginname)){
				uLoginname = "%"+ uLoginname+"%";
			}
			if(StringUtils.isNotBlank(uLoginname)){
				crita.andULoginnameLike(uLoginname);
			}
			list = umapper.selectByExample(example);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Integer AddUser(User user) {
		// TODO Auto-generated method stub
		Integer flag = 0;
		try {
			flag = umapper.insertSelective(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public User FindUseById(Integer uId) {
		// TODO Auto-generated method stub
		List<User> user = null;
		try {
			UserExample example = new UserExample();
			UserExample.Criteria crita = example.createCriteria();
			crita.andUIdEqualTo(uId);
			user = umapper.selectByExample(example);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user.get(0);
	}

	@Override
	public boolean DelUserById(Integer uId) {
		// TODO Auto-generated method stub
		Integer flag = 0;
		try {
			UserExample example = new UserExample();
			UserExample.Criteria crita = example.createCriteria();
			crita.andUIdEqualTo(uId);
			flag = umapper.deleteByExample(example);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(flag>0){
			return true;
		}else{
			return false;	
		}
	}

	@Override
	public Integer UpdateUseById(User user, Integer uId) {
		// TODO Auto-generated method stub
		Integer flag = 0;
		try {
			UserExample example = new UserExample();
			UserExample.Criteria crita = example.createCriteria();
			crita.andUIdEqualTo(uId);
			flag = umapper.updateByExample(user, example);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

}
